package com.example.photograph.controller;

import com.example.photograph.domain.AjaxResult;
import com.example.photograph.domain.SysDepartment;
import com.example.photograph.domain.SysUser;
import com.example.photograph.page.TableDataInfo;
import com.example.photograph.service.ISysDepartmentService;
import com.example.photograph.service.ISysUserDepService;
import com.example.photograph.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门管理Controller
 * 
 * @author wuyj
 * @date 2021-03-06
 */
@Controller
@RequestMapping("/photograph/department")
public class SysDepartmentController extends BaseController
{
    private String prefix = "photograph/department";

    @Autowired
    private ISysDepartmentService sysDepartmentService;
    @Autowired
    private ISysUserDepService sysUserDepService;
    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("photograph:department:view")
    @GetMapping()
    public String department()
    {
        return prefix + "/department";
    }

    /**
     * 查询部门管理列表
     */
    @RequiresPermissions("photograph:department:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDepartment sysDepartment)
    {
        startPage();
        List<SysDepartment> list = sysDepartmentService.selectSysDepartmentList(sysDepartment);
        return getDataTable(list);
    }

    /**
     * 新增部门管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存部门管理
     */
    @RequiresPermissions("photograph:department:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDepartment sysDepartment)
    {
        return toAjax(sysDepartmentService.insertSysDepartment(sysDepartment));
    }

    /**
     * 修改部门管理
     */
    @GetMapping("/edit/{departmentId}")
    public String edit(@PathVariable("departmentId") Long departmentId, ModelMap mmap)
    {
        SysDepartment sysDepartment = sysDepartmentService.selectSysDepartmentById(departmentId);
        mmap.put("sysDepartment", sysDepartment);
        return prefix + "/edit";
    }

    /**
     * 修改保存部门管理
     */
    @RequiresPermissions("photograph:department:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDepartment sysDepartment)
    {
        return toAjax(sysDepartmentService.updateSysDepartment(sysDepartment));
    }

    /**
     * 删除部门管理
     */
    @RequiresPermissions("photograph:department:remove")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysDepartmentService.deleteSysDepartmentByIds(ids));
    }

    /**
     * 分配员工
     */
    @RequiresPermissions("photograph:department:authUser")
    @GetMapping("/authUser/{departmentId}")
    public String authUser(@PathVariable("departmentId") Long departmentId, ModelMap mmap)
    {
        mmap.put("department", sysDepartmentService.selectSysDepartmentById(departmentId));
        return prefix + "/authUser";
    }

    /**
     * 查询已分配用户角色列表
     */
    @PostMapping("/authUser/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = sysUserService.selectAllocatedList(user);
        return getDataTable(list);
    }

    /**
     * 取消部门
     */
    @PostMapping("/authUser/cancel")
    @ResponseBody
    public AjaxResult cancelAuthUser(Long userId)
    {
        return toAjax(sysUserDepService.deleteSysUserDepById(userId));
    }

    /**
     * 选择用户
     */
    @GetMapping("/authUser/selectUser/{departmentId}")
    public String selectUser(@PathVariable("departmentId") Long departmentId, ModelMap mmap)
    {
        mmap.put("department", sysDepartmentService.selectSysDepartmentById(departmentId));
        return prefix + "/selectUser";
    }

    /**
     * 查询未分配用户角色列表
     */
    @PostMapping("/authUser/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = sysUserService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("photograph:department:authUser")
    @PostMapping("/authUser/selectAll")
    @ResponseBody
    public AjaxResult selectAuthUserAll(Long departmentId, String userIds)
    {
        return toAjax(sysUserDepService.insertAuthUsers(departmentId, userIds));
    }
}
